package fr.pud.client.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.HashSet;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;

import fr.pud.client.controller.address.CloseSuggestionsAddressesFrame;
import fr.pud.client.controller.address.GetUserAddressChoices;
import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.model.Address;

/**
 * Apr 8, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         AddressSuggestionsFrame.java
 * @see geocoding(String address)
 *      This class implements a JFrame which allows to the user to choose
 *      the suitable address between
 *      a list of addresses (which was imported from a csv)
 */
public class AddressSuggestionsFrame extends JFrame {
    private static final long         serialVersionUID = -8126847999601256924L;
    /**
     * A list of list of addresses.
     * Each list of addresses came from the the function
     */
    private HashSet<HashSet<Address>> suggestionsAddress;
    /**
     * An ArrayList<JComboBox> to get each choice of the user for each address.
     */
    private ArrayList<JComboBox>      userChoices;
    /**
     * A JButton to validate the choice of the user.
     */
    private JButton                   validate;
    /**
     * A JButton to close the frame without validate the choice.
     */
    private JButton                   close;

    /**
     * Create a JFrame which allows the user to choose between several
     * addresses.
     * @param suggestionsList
     *            , represents the propositions of addresses.
     */
    public AddressSuggestionsFrame(HashSet<HashSet<Address>> suggestionsList) {
        super(
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.TITLE_ADDRESSES_SUGGESTIONS_FRAME));
        this.suggestionsAddress = suggestionsList;
        this.userChoices = new ArrayList<JComboBox>();
        this.fillComboBox();
        this.prepareLayout();
        this.prepareButton();
        this.pack();
    }

    /**
     * This method fills each JComboBox with all the suggested addresses.
     */
    private void fillComboBox() {
        int index = 1;
        JPanel globalPanel = new JPanel();
        globalPanel.setLayout(new BoxLayout(globalPanel, BoxLayout.Y_AXIS));
        for (HashSet<Address> listAddress : this.suggestionsAddress) {
            JPanel pan = new JPanel();
            pan.setLayout(new BoxLayout(pan, BoxLayout.PAGE_AXIS));
            JLabel title = new JLabel(
                    LanguagesConstants.LANGUAGE
                            .getString(LanguagesConstants.ADDRESS)
                            + " n°"
                            + index++);
            pan.add(title);
            JComboBox comboBox = new JComboBox(listAddress.toArray());
            if (listAddress.size() == 1) {
                comboBox.setForeground(Color.GREEN);
            }
            else if (listAddress.size() == 2) {
                comboBox.setForeground(Color.ORANGE);
            }
            else {
                comboBox.setForeground(Color.RED);
            }
            this.userChoices.add(comboBox);
            pan.add(comboBox);
            globalPanel.add(pan);
        }
        JScrollPane scroll = new JScrollPane(globalPanel);
        this.getContentPane().add(scroll);
    }

    /**
     * @return the suggested addresses.
     */
    public HashSet<HashSet<Address>> getSuggestionsAddress() {
        return this.suggestionsAddress;
    }

    /**
     * @return all the JComboBox used.
     */
    public ArrayList<JComboBox> getUserChoices() {
        return this.userChoices;
    }

    /**
     * Prepare the button validate and close.
     */
    private void prepareButton() {
        this.validate = new JButton(new GetUserAddressChoices(
                this.getUserChoices(), this));
        this.close = new JButton(new CloseSuggestionsAddressesFrame(this));
        JPanel pan = new JPanel(new FlowLayout());
        pan.add(this.close);
        pan.add(this.validate);
        this.getContentPane().add(pan, BorderLayout.SOUTH);
    }

    /**
     * Prepare the JFrame.
     */
    private void prepareLayout() {
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.setVisible(true);
        this.setBackground(Color.LIGHT_GRAY);
        this.setLocation(JMapViewerExtended.getJMapViewer()
                .getLocationOnScreen().x, JMapViewerExtended.getJMapViewer()
                .getLocationOnScreen().y);
    }
}
